package com.servlets;

import com.domain.Movies;
import com.domain.service.MoviesService;
import com.domain.utils.GetConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jcp.xml.dsig.internal.dom.Utils;

public class MoviesListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        MoviesService moviesService = new MoviesService();

        List<Movies> moviesList = moviesService.getAll();

        Collections.sort(moviesList);

        PrintWriter out = null;

        try {

            response.setContentType("text/html");

            out = response.getWriter();

            out.println("<html>");
            out.println("<title>AbhiApp | MoviesList</title>");
            out.println("<head></head>");
            out.println("<body bgcolor='gray'>");
            out.println("<h1 align='center'>All Movies</h1><hr/><br>");
            out.println("<form method='post' action='MoviesListServlet'>");
            out.println("<table bgcolor='white'align='center' cellspacing='10'>");
            out.println("</form>");
            out.println("<th><input type='submit' value='delete'></th><th>Number</th><th>Movie Name</th><th>Language</th><th>Category</th><th>Description</th><th>year</th>");

            int count = 1;
            for (int i = 0; i < moviesList.size(); i++) {

                out.println("<tr>");
                out.println("<td><input type='checkbox' name='deleteEach' value='" + moviesList.get(i).getMovieName() + "'></td>");
                out.println("<td>" + count + "</td>");
                out.println("<td>" + moviesList.get(i).getMovieName() + "</td>");
                out.println("<td>" + moviesList.get(i).getLanguage() + "</td>");
                out.println("<td>" + moviesList.get(i).getCategory() + "</td>");
                out.println("<td>" + moviesList.get(i).getDescription() + "</td>");
                out.println("<td>" + moviesList.get(i).getYear() + "</td>");
                out.println("<td><a href='DeleteMovieServlet?movieName=" + moviesList.get(i).getMovieName() + "'>Remove</a></td>");
                out.println("<td><a href='UpdateMovieServlet?movieName=" + moviesList.get(i).getMovieName() + "'>Update</a></td></tr>");

                count++;
            }

            out.println("</table><br><br>");
            out.println("<p align='center'><a  href='index.html'>| HOME |</a><a  href='createNewMovie.html'>| CREATE A NEW MOVIE |</a></p>");
            out.println("</body>");
            out.println("</html>");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @Override
    @SuppressWarnings("empty-statement")
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String[] deleteList = request.getParameterValues("deleteEach");


        response.setContentType("text/html");

        PrintWriter out = null;

        if (deleteList == null) {
            return;
        }

        

        Connection connection = null;

        PreparedStatement preparedStatement = null;

        try {

            for (int i = 0; i < deleteList.length; i++) {

                connection = GetConnection.setConnection();

                preparedStatement = connection.prepareStatement("Delete from movies where movieName=?");
                preparedStatement.setString(1, deleteList[i]);
                preparedStatement.executeUpdate();

                connection.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }


        }
     response.sendRedirect("MoviesListServlet");
    }
}
